﻿using DFGK.IService;
using DFGK.Service;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Enum;
using DFGK_WMS.Model.Models;
using Microsoft.AspNetCore.Connections.Features;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using static System.Runtime.CompilerServices.RuntimeHelpers;
using System.Xml.Linq;
using DFGK_WMS.Model.Dto;

namespace DFGK_WMS.WebAPI.Controllers.PC.出库管理
{
    /// <summary>
    /// 出库管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "PC")]
    public class ShippingOrderDetailController : BaseController
    {
        private readonly IConfiguration configuration;
        private readonly IShippingOrderDetailService shippingOrderDetailService;
        private readonly IShippingReplenishmentService shippingReplenishmentService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="environment"></param>
        /// <param name="shippingOrderDetailService"></param>
        /// <param name="shippingReplenishmentService"></param>
        /// <param name="configuration"></param>
        public ShippingOrderDetailController(IWebHostEnvironment environment, IShippingOrderDetailService shippingOrderDetailService, IShippingReplenishmentService shippingReplenishmentService, IConfiguration configuration) : base(environment)
        {
            this.shippingOrderDetailService = shippingOrderDetailService;
            this.shippingReplenishmentService = shippingReplenishmentService;
            this.configuration = configuration;
        }

        /// <summary>
        /// 添加出库单
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddShippingOrderDetail(ShippingOrderDetail m)
        {
            if (string.IsNullOrEmpty(m.OrderSn))
            {
                return Ok(BaseResult.Result("400", "单号不能为空！"));
            }
            string message;
            m.CreateUserId = base.BaseGetNowUserId();
            m.CreateUserName = base.BaseGetNowUserName();
            bool result = shippingOrderDetailService.AddShippingOrderDetail(m, out message);
            if (result)
            {
                return Ok(BaseResult.Result(result, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(result, "添加失败！" + message, "500"));
        }

        /// <summary>
        /// 删除出库单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelShippingOrderDetail(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            string message;
            bool IsOK = shippingOrderDetailService.DelShippingOrderDetail(id, out message);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！" + message, "500"));
        }

        /// <summary>
        /// 根据主键获取对象信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet, Obsolete("该方法已被已弃用")]
        public IActionResult GetShippingOrderDetailById(long id)
        {
            return Ok("弃用了你看不到吗？还点");
            //if (id <= 0)
            //{
            //    return Ok(BaseResult.Result("400", "Id有误！"));
            //}
            //var obj = shippingOrderDetailService.GetShippingOrderDetailById(id);
            //if (obj != null)
            //{
            //    return Ok(BaseResult.Result(obj, "查询成功！", "200", 0));
            //}
            //return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 根据主键获取对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetShippingMaterialLookDtoById(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var obj = shippingOrderDetailService.GetShippingMaterialLookDtoById(id);
            if (obj != null)
            {
                return Ok(BaseResult.Result(obj, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 获取出库单列表
        /// </summary>
        /// <param name="ordersn"></param>
        /// <param name="materialkey"></param>
        /// <param name="status"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet, Obsolete("该方法已被已弃用")]
        public IActionResult GetShippingOrderDetailList(string? ordersn, string? materialkey, int status, int pageIndex, int pageSize)
        {
            return Ok("弃用了你看不到吗？还点");
            //if (pageIndex <= 0 || pageSize <= 0)
            //{
            //    return Ok(BaseResult.Result("400", "分页参数有误！"));
            //}
            //int totalCount;
            //var list = shippingOrderDetailService.GetShippingOrderDetailList(ordersn, materialkey, status, pageIndex, pageSize, out totalCount);
            //if (list != null)
            //{
            //    return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            //}
            //return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 获取出库单列表
        /// </summary>
        /// <param name="ordersn"></param>
        /// <param name="materialkey"></param>
        /// <param name="status"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetShippingOrderDetailLockDtoList(string? ordersn, string? materialkey, int status, int pageIndex, int pageSize)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页参数有误！"));
            }
            int totalCount;
            var list = shippingOrderDetailService.GetShippingOrderDetailLockDtoList(ordersn, materialkey, status, pageIndex, pageSize, out totalCount);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }
        /// <summary>
        /// 获取出库库存物料
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="mname"></param>
        /// <param name="arecode"></param>
        /// <param name="SuplierName"></param>
        /// <param name="StockName"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetInventoryByMaterialAll(int pageIndex, int pageSize, string? mname, string? arecode, string? SuplierName, string? StockName)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页参数有误！"));
            }
            int totalCount;
            var list = shippingOrderDetailService.GetInventoryByMaterialAll(pageIndex, pageSize, mname, arecode,  SuplierName,  StockName, out totalCount);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }


        /// <summary>
        /// 一键选定出库物料
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="mid"></param>
        /// <param name="outqty"></param>
        /// <param name="arecode"></param>
        /// <param name="batchno"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetSelectInventoryMaterialAll(int pageIndex, int pageSize, long? mid, decimal? outqty, string? arecode, string? batchno)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页参数有误！"));
            }
            if (outqty == null || outqty <= 0)
            {
                return Ok(BaseResult.Result("400", "请输入出库数量！"));
            }
            int totalCount;
            var list = new List<ShippingSelectMaterialDto>();
            bool IsReplenishment = configuration.GetValue<bool>("IsReplenishment");
            if(IsReplenishment)
                list = shippingOrderDetailService.GetSelectInventoryMaterialAll(pageIndex, pageSize, mid, outqty, arecode, out totalCount);
            else
                list = shippingOrderDetailService.GetSelectInventoryMaterialAllNo(pageIndex, pageSize, mid, outqty, arecode,batchno, out totalCount);

            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "库存数量不足！", "500", 0));
        }

        /// <summary>
        /// 修改出库单
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdShippingOrderDetail(ShippingOrderDetail m)
        {
            if (m.Id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            if (string.IsNullOrEmpty(m.OrderSn))
            {
                return Ok(BaseResult.Result("400", "单号不能为空！"));
            }
            string message;
            m.ModifyUserId = base.BaseGetNowUserId();
            m.ModifyUserName = base.BaseGetNowUserName();
            bool result = shippingOrderDetailService.UpdShippingOrderDetail(m, out message);
            if (result)
            {
                return Ok(BaseResult.Result(result, "修改成功！", "200"));
            }
            return Ok(BaseResult.Result(result, "修改失败！" + message, "500"));
        }

        /// <summary>
        /// 出库单据类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetEmOutBillType()
        {
            var list = BaseGetEnum.GetCodeRulesList<EmOutBillType>();

            return Ok(list);
        }

        /// <summary>
        /// 根据单号获取打包台数据
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetShippingOrderDetailByOrder(string order)
        {
            if (string.IsNullOrEmpty(order))
            {
                return Ok(BaseResult.Result("400", "单号不能为空！"));
            }
            var list = shippingOrderDetailService.GetShippingOrderDetailByOrder(order);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 补货显示
        /// </summary>
        /// <param name="ordersn"></param>
        /// <param name="status"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetShippingReplenishmentList(string? ordersn, int status, int pageIndex, int pageSize)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页参数有误！"));
            }
            int totalCount;
            var list = shippingReplenishmentService.GetShippingReplenishmentList(ordersn, status, pageIndex, pageSize, out totalCount);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 补货
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult StartReplenishment(ReplenishmentDto m)
        {
            m.NowUserId = base.BaseGetNowUserId();
            m.NowUserName = base.BaseGetNowUserName();
            string message;
            bool result = shippingReplenishmentService.StartReplenishment(m, out message);
            if (result)
            {
                return Ok(BaseResult.Result(result, "操作成功！", "200"));
            }
            return Ok(BaseResult.Result(result, "操作失败！" + message, "500"));
        }
    }
}
